package com.studyyoun.demoandroidapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.MethodChannel;

//代码清单9-19
//FlutterViewActivity.java
public class FlutterViewActivity extends AppCompatActivity {
	
	private FlutterEngine mFlutterEngine;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_flutter_view);
		// 通过FlutterView引入Flutter编写的页面
		mFlutterEngine = new FlutterEngine(this);
		mFlutterEngine.getDartExecutor().executeDartEntrypoint(
				DartExecutor.DartEntrypoint.createDefault()
		);
		mFlutterEngine.getNavigationChannel().setInitialRoute("test");

		// 通过FlutterView引入Flutter编写的页面
		FlutterView flutterView = new FlutterView(this);
		FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);

		//Android页面布局
		FrameLayout flContainer = findViewById(R.id.fl_container);
		flContainer.addView(flutterView, lp);
		// 关键代码，将Flutter页面显示到FlutterView中
		flutterView.attachToFlutterEngine(mFlutterEngine);

        //消息通道
		MethodChannel methodChannel =
				new MethodChannel(mFlutterEngine.getDartExecutor(),
						"com.example.flutter/native");
		//或
		MethodChannel methodChannel1 =
				new MethodChannel(mFlutterEngine.getDartExecutor().getBinaryMessenger(),
						"com.example.flutter/native");
	}
	@Override
	protected void onResume() {
		super.onResume();
		mFlutterEngine.getLifecycleChannel().appIsResumed();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		mFlutterEngine.getLifecycleChannel().appIsInactive();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		mFlutterEngine.getLifecycleChannel().appIsPaused();
	}
}